//
//  SEImageBrowser.swift
//  SmartEyeForDoctor
//
//  Created by QDSG on 2021/5/15.
//

import UIKit
import Kingfisher

class SEImageBrowser: SEBaseViewController {
    
    private var currentImageView: UIImageView?
    private var originalUrl: String?
    
    convenience init(imageView: UIImageView? = nil, originalUrl: String? = nil) {
        self.init()
        self.currentImageView = imageView
        self.originalUrl = originalUrl
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .black
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        makeUI()
    }
    
    override func setupNavBar() {
        super.setupNavBar()
        
        navigationController?.navBarStyle(.clear)
    }
    
    private func makeUI() {
        scrollView.tapImageView = currentImageView
        
        if originalUrl == nil {
            scrollView.imageView?.image = currentImageView?.image
        } else {
            scrollView.imageView?.kf.indicatorType = .activity
            scrollView.imageView?.kf.setImage(urlString: originalUrl, placeholder: currentImageView?.image)
        }
        
        scrollView.tapCallBack = { [weak self] in
            self?.dismiss(animated: false, completion: nil)
        }
    }

    // MARK: - getter
    
    private lazy var scrollView: SEImageBrowserScrollView = {
        let view = SEImageBrowserScrollView(frame: self.view.bounds)
        self.view.addSubview(view)
        return view
    }()

}
